var express = require('express');

var app = express();
var crawler = require('./crawler/crawlerEngine');

app.get('/', function (req, resp, next) {
    var engine = crawler.engine
    engine.queue([{
        uri: 'http://dig.chouti.com/',

        callback: function (error, res, done) {
            if(error){
                return next(error);
            }else{
                var items = [];
                var $ = res.$
                $('#content-list .part2').each(function (idx, element) {
                    var $element = $(element);
                    items.push({
                        title: $element.attr('share-title'),
                        href: $element.attr('href'),
                        img: $element.attr('share-pic')
                    });
                });

                resp.send(items);
            }
            done();
        }
    }])
});

app.listen(3000, function () {
    console.log('app is listening at port 3000');
});